package upload;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * @author Iwan Cuche
 * @date: 15.05.2014
 */


/**
 * Class containing the uploadImage() method
 *
 */
public class FileUploader {

	/**
	 * This method reads a File and saves it
	 * 
	 * @param String fileName
	 * @param InputStream stream
	 */
	public static File uploadImage(String fileName, InputStream stream)
			throws IOException {

		File file = new File(fileName);
		try {
			OutputStream os = new FileOutputStream(file);

			int data;
			
			// Read the stream
			while ((data = stream.read()) != -1) {
				os.write(data);
			}
			// Flushing the stream
			os.flush();
			// Closing the stream
			os.close();

			System.out.println("Uploaded file successfully saved in "
					+ file.getAbsolutePath());

		} catch (IOException e) {
			// Message shown when File not found
			e.getLocalizedMessage();
			throw e;
		}
		return file;

	}// END OF uploadImage
}// END OF CLASS